package com.powernode.business.service.impl;

import com.alibaba.fastjson.JSON;
import com.powernode.business.vo.AreaVo;
import com.powernode.common.core.domain.entity.SysDept;
import com.powernode.common.core.domain.model.LoginUser;
import com.powernode.system.mapper.SysDeptMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.powernode.business.mapper.CustomerMapper;
import com.powernode.business.domain.Customer;
import com.powernode.business.service.CustomerService;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class CustomerServiceImpl implements CustomerService{

    @Resource
    private CustomerMapper customerMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return customerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Customer record) {
        return customerMapper.insert(record);
    }

    @Override
    public int insertSelective(Customer record) {
        return customerMapper.insertSelective(record);
    }

    @Override
    public Customer selectByPrimaryKey(Integer id) {
        return customerMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Customer record) {
        return customerMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Customer record) {
        return customerMapper.updateByPrimaryKey(record);
    }

    /**
     * 分页查询用户
     * @param customer
     * @param deptId
     * @return
     */
    @Override
    public List<Customer> selectCustomerList(Customer customer, Long deptId) {
        return customerMapper.selectCustomerList(customer,deptId);
    }

    /**
     * 添加用户操作
     * @param customer
     */
    @Override
    public void addUser(Customer customer) {
        this.customerMapper.insertSelective(customer);
    }



    /**
     * 删除用户
     * @param ids
     * @return
     */
    @Override
    public int deleteCustomerById(Long[] ids) {
        return customerMapper.deleteCustomerById(ids);
    }


    @Override
    public List<Customer> selectCustomerDept() {
        return customerMapper.selectCustomerDept();
    }

    /**
     * 添加用户
     * @param customer
     * @param loginUser
     * @return
     */
    @Override
    public int addCustomer(Customer customer, LoginUser loginUser) {
        log.info("新增用户{},当前用户id是{}", JSON.toJSONString(customer), loginUser.getUserId());
        customer.setCreateBy(loginUser.getUsername());
        customer.setCreateTime(new Date());
        customer.setUpdateBy(loginUser.getUsername());
        customer.setUpdateTime(new Date());
        //处理部门名称
        SysDept sysDept = sysDeptMapper.selectDeptById(customer.getDeptId().longValue());
        customer.setDeptName(sysDept.getDeptName());
        //插入数据库
        return customerMapper.insert(customer);
    }

    /**
     * 修改客户信息
     * @param customer
     * @param loginUser
     * @return
     */
    @Override
    public int updateCustomer(Customer customer, LoginUser loginUser) {
        log.info("修改客户信息{},当前用户的id是", JSON.toJSONString(customer),loginUser.getUserId());
        customer.setUpdateBy(loginUser.getUsername());
        customer.setUpdateTime(new Date());
        //修改需要处理部门
        SysDept sysDept = sysDeptMapper.selectDeptById(customer.getDeptId().longValue());
        customer.setDeptName(sysDept.getDeptName());
        //更新数据库
        return customerMapper.updateByPrimaryKeySelective(customer);
    }

    /**
     * 删除用户
     * 只能逻辑删除，在判断中做删除
     * @param ids
     * @param loginUser
     * @return
     */
    @Override
    public int deleteCustomer(List<Integer> ids, LoginUser loginUser) {
        log.info("删除客户的ids为{},当前用户的id是",ids.toString(),loginUser.getUserId());
        if (CollectionUtils.isEmpty(ids)){
            //用工具类
            return 0;
        }
        //
        return customerMapper.deleteCustomer(ids,loginUser.getUsername());
    }


    /**
     * 租车管理
     * 检验用户是否存在
     * @param identity
     * @return
     */
    @Override
    public Customer selectByIdentity(String identity) {
        return customerMapper.selectByIdentity(identity);
    }

    @Override
    public List<AreaVo> selectArea() {
        return customerMapper.selectArea();
    }
}
